package cn.oa.service.impl;

import cn.oa.dao.EmployeeDao;
import cn.oa.dao.impl.AttendanceDaoImpl;
import cn.oa.dao.impl.EmployeeDaoImpl;
import cn.oa.entity.Employee;
import cn.oa.service.EmployeeService;
import cn.oa.util.DatabaseUtil;
import cn.oa.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeServiceImpl implements EmployeeService{
    @Override
    public Employee findEmployee(String employeeNo, String password) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new EmployeeDaoImpl(conn).findEmployee(employeeNo,password);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public void getEmployeesByPage(String name, Page pageObj) throws SQLException {
        Connection conn = null;
        try {
            conn = DatabaseUtil.getConnection();
            EmployeeDao empDao = new EmployeeDaoImpl(conn);
            int totalCount = empDao.getEmployeesCount(name);
            pageObj.setTotalCount(totalCount);
            if (totalCount > 0) {
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                }
                List<Employee> empList = empDao.getEmployeesByPage(name, pageObj.getCurrPageNo(), pageObj.getPageSize());
                pageObj.setEmployeeList(empList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setEmployeeList(new ArrayList<Employee>());
            }

        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int updateEmployee(Employee employee) throws SQLException {
        Connection conn= null;
        int result;
        try{
            conn = DatabaseUtil.getConnection();
            EmployeeDao employeeDao = new EmployeeDaoImpl(conn);
            return result = employeeDao.updateEmployee(employee);
        }catch (SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int addNewEmployee(Employee employee) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);
            EmployeeDao employeeDao = new EmployeeDaoImpl(conn);
            if(employeeDao.getEmployeeByNo(employee.getEmployeeNo())>0){
                //该员工号已存在
                result = -1;
            }else{
                //新增员工
                result = employeeDao.addNewEmployee(employee);
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public Employee getEmployeeById(int id) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new EmployeeDaoImpl(conn).getEmployeeById(id);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int deleteEmployeeById(int id) throws SQLException {
        Connection conn = null;
        int result = 0;
        try{
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);
            //删除员工的所有考核记录
            new AttendanceDaoImpl(conn).deleteAttendanceByEmployeeId(id);
            //删除员工的信息
            result = new EmployeeDaoImpl(conn).deleteEmployeeById(id);
            conn.commit();
        }catch(SQLException e){
            e.printStackTrace();
            if(conn!=null){
                try{
                    conn.rollback();
                    result = -1;
                }catch(SQLException e1){
                    e.printStackTrace();
                }
            }
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
        return result;
    }

    @Override
    public int updatePassword(int id, String pwd) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new EmployeeDaoImpl(conn).updatePasswordById(id,pwd);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }
}


